package nku.csc360;

import java.util.Date;


public class Account {
  private int id;
  private double balance;
  private double annualInterestRate;
  private java.util.Date dateCreated;


  public Account() {
    dateCreated = new java.util.Date();
  }

  public Account(int id, double balance, double annualInterestRate, Date dateCreated) {
    this.id = id;
    this.balance = balance;
    this.annualInterestRate = annualInterestRate;
    this.dateCreated = dateCreated;

  }

  public int getId() {
    return this.id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public double getBalance() {
    return balance;
  }

  public void setBalance(double balance) {
    this.balance = balance;
  }
  
  public double getAnnualInterestRate() {
    return annualInterestRate;
  }

  public void setAnnualInterestRate(double annualInterestRate) {
    this.annualInterestRate = annualInterestRate;
  }

  public java.util.Date getDateCreated() {
    return dateCreated;
  }

  public double getMonthlyInterest() {
    return balance * (annualInterestRate / 1200);
  }

  public void withdraw(double amount) {
    balance -= amount;
  }

  public void deposit(double amount) {
    balance += amount;
  }

  public void addMonthlyInterest() {
	deposit(getBalance() + getMonthlyInterest());
  }
  
  public String toString() {
    return "Account " + getId() + " (created " + getDateCreated() + ") has balance " + getBalance();
  }
  
  //Added this to clear saving accounts withdrawals
  public static void clears(Account[] account){
		//Local Variable used to iterate through the loop for each account to clear and add interest
  		int count = account.length;
  		
  		//Loop used to add interest and clear withdrawals
  		while(count > 0){
  			
  			//Setting up counter to be used in the values
  			count--;
  			
  			//Only add interest if the account in question has a positive balance 
  			if(account[count].getBalance() > 0)
  				account[count].addMonthlyInterest();
			
			//If account is an instance of saving reset the no. of withdrawals the account has...
			if (account[count] instanceof Saving){
				Saving temp = (Saving) account[count];
				temp.withdrawals = Saving.bankwithdrawals;
			}
  		}
	}
}